local juesizhi = fk.CreateSkill{
  name = "lzsj__juesizhi",
}

Fk:loadTranslationTable{
  ["lzsj__juesizhi"] = "决死志",
  ["@@juesizhi-round"] = "决死志<font color=\"#E8251E\"><b>发动</b></font>",
  ["#lzsj__juesizhi"] = "请在选项中选择失去牌的角色，然后指定目标角色",
  ["#lzsj__juesizhi-choose"] = "请选择一名符合条件的角色",
  ["#juesizhi-choice"] = "请选择失去的技能",
  [":lzsj__juesizhi"] = "你可将一名角色的最后一张手牌/场上唯一的牌/两者同时当一张【出其不意】/【闪】/【无懈可击】使用。若你以此法选择其他角色的牌且不为此轮首次，你失去一个技能。",
  ["juesizhi_choice"] = "选择%src",

  ["$lzsj__juesizhi1"] = "最后一颗子弹留给我？不，留给关东军司令的脑袋！",
  ["$lzsj__juesizhi2"] = "要我投降？中国人都投降了还有中国吗？",
}

juesizhi:addAcquireEffect(function (self, player, is_start)
  local cards = table.map({"unexpectation", "jink", "nullification"}, function (n)
    if table.contains(Fk:getAllCardNames("bt"), "lvshi__"..n) then
      return player.room:printCard("lvshi__"..n).id
    else
      return player.room:printCard(n).id
    end
  end)
  player.room:setPlayerMark(player, juesizhi.name, cards)
end)

juesizhi:addEffect("viewas", {
  prompt = "#lzsj__juesizhi",
  pattern = "jink,nullification,unexpectation",
  expand_pile = function (self, player)
    return player:getTableMark(juesizhi.name)
  end,
  interaction = function (self, player)
    local players = {}
    for _, p in ipairs(Fk:currentRoom().alive_players) do
      if p:getHandcardNum() == 1 or #p:getCardIds("ej") == 1 then
        table.insertIfNeed(players, "juesizhi_choice:"..p.id)
      end
    end
    return UI.ComboBox { choices = players }
  end,
  card_filter = function (self, player, to_select, selected)
    if not self.interaction.data then return end
    if #selected == 0 and table.contains(player:getTableMark(juesizhi.name), to_select) then
      local id = tonumber(string.sub(self.interaction.data, 17))
      local p = Fk:currentRoom():getPlayerById(id)
      local names = {}
      if p:getHandcardNum() == 1 then
        table.insert(names, "unexpectation")
      end
      if #p:getCardIds("ej") == 1 then
        table.insert(names, "jink")
      end
      if p:getHandcardNum() == 1 and #p:getCardIds("ej") == 1 then
        table.insert(names, "nullification")
      end
      return table.contains(names, Fk:getCardById(to_select).trueName)
    end
  end,
  view_as = function (self, player, cards)
    if #cards ~= 1 or not self.interaction.data then return end
    local card = Fk:cloneCard(Fk:getCardById(cards[1]).name)
    card.skillName = juesizhi.name
    return card
  end,
  enabled_at_play = function (self, player)
    return table.find(Fk:currentRoom().alive_players, function (p)
      return p:getHandcardNum() == 1
    end)
  end,
  enabled_at_response = function(self, player, response)
    return not response
  end,
  before_use = function (self, player, use)
    local id = tonumber(string.sub(self.interaction.data, 17))
    local to = player.room:getPlayerById(id)
    local cards
    if use.card.trueName == "unexpectation" then
      cards = to:getCardIds("h")
    elseif use.card.trueName == "jink" then
      cards = to:getCardIds("ej")
    elseif use.card.trueName == "nullification" then
      cards = to:getCardIds("hej")
    end
    use.card:addSubcards(cards)
  end,
  after_use = function (self, player, use)
    local room = player.room
    local id = tonumber(string.sub(self.interaction.data, 17))
    local to = player.room:getPlayerById(id)
    if player ~= to then
      if player:getMark("@@juesizhi-round") == 0 then
        room:addPlayerMark(player, "@@juesizhi-round")
      else
        local skills = {}
        for _, skill in ipairs(player.player_skills) do
          if skill:isPlayerSkill(player) and skill.visible then
            table.insert(skills, skill.name)
          end
        end
        local choice = room:askToChoice(player, { choices = skills, skill_name = juesizhi.name, prompt = "#juesizhi-choice" })
        room:handleAddLoseSkills(player, "-"..choice, nil, true, false)
      end
    end
  end,
})

return juesizhi